﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Windows.Data;

namespace CFR.Silverlight.Scheduling
{
    public class DayViewColumn : Control
    {
        //internal ItemsControl AllDayEventsItemsControl { get; private set; }
        internal Border AllDayEventsBorder { get; private set; }
        internal StackPanel AllDayEventsStackPanel { get; private set; }

        private DayViewColumnPresenter _ParentPresenter;
        internal DayViewColumnPresenter ParentPresenter
        {
            get
            {
                return _ParentPresenter;
            }
            set
            {
                _ParentPresenter = value;
                value.ParentDayView.Scheduler.Events.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Events_CollectionChanged);
            }
        }

        void Events_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            Refresh();
        }

        #region Date (DependencyProperty)

        /// <summary>
        /// Gets or sets the date relevant to to this DayViewItem
        /// </summary>
        public DateTime Date
        {
            get { return (DateTime)GetValue(DateProperty); }
            set { SetValue(DateProperty, value); Refresh(); }
        }
        public static readonly DependencyProperty DateProperty =
            DependencyProperty.Register("Date", typeof(DateTime), typeof(DayViewColumn),
              new PropertyMetadata(null));

        #endregion


        public DayViewColumn()
            : base()
        {
            this.DefaultStyleKey = typeof(DayViewColumn);
            this.UseLayoutRounding = false;


        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            //AllDayEventsItemsControl = (ItemsControl)GetTemplateChild("AllDayEventsItemsControl");
            //AllDayEventsBorder = (Border)GetTemplateChild("AllDayEventsBorder");

            AllDayEventsStackPanel = (StackPanel)GetTemplateChild("AllDayEventsStackPanel");


            Refresh();
        }

        internal void Refresh()
        {
            //if (AllDayEventsItemsControl != null)
            //{
            //    AllDayEventsItemsControl.ItemsSource = ParentPresenter.ParentDayView.Scheduler.Events.Where(e => e.IsAllDayEvent && e.StartDate.Date == Date.Date);

            //}

            if (AllDayEventsStackPanel == null)
                return;

            AllDayEventsStackPanel.Children.Clear();

            var q = ParentPresenter.ParentDayView.Scheduler.Events.Where(e => e.IsAllDayEvent && e.StartDate.Date == Date.Date && e.Calendar.IsShownInScheduler);

            foreach (Event e in q)
            {
                EventItem ei = new EventItem(){
                    Event = e,
                    Title = e.Name,
                    View = ParentPresenter.ParentDayView,
                    Foreground = new SolidColorBrush(Colors.White),
                    Background = e.Calendar.Background,
                    Margin=new Thickness(3,1,3,1),
                    TitleContentTemplate = ParentPresenter.ParentDayView.Scheduler.EventTitleContentTemplate
                    
                };
                AllDayEventsStackPanel.Children.Add(ei);
            }
        }
    }
}
